﻿using BJ.AuthDomain.IService;
using Microsoft.AspNetCore.Mvc;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DynamicApi;
using Syspetro.Core.GoogleAuth;
using Syspetro.Deploy.AuthorizationReq;
using Syspetro.Deploy.Jwt;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace BJ.AuthApp.Admin
{
    public class EncryptionApp: BaseDynamicApi
    {
        readonly IGoogleAuthService _googleAuth;
        public EncryptionApp(IGoogleAuthService googleAuth)
        {
            _googleAuth = googleAuth;
        }
        /// <summary>
        /// 生成RSA密钥和公钥(默认实现为HSA)
        /// </summary>
        /// <returns></returns>
        [RoleLevel(10)]
        public SpActionResult<RSAParameters> GetKey()
        {
            string keyDir = Directory.GetCurrentDirectory();
            if (RSATokenHelper.TryGetKeyParameters(keyDir, false, out RSAParameters keyParams) == false)
            {
                keyParams = RSATokenHelper.GenerateAndSaveKey(keyDir, false);
            }
            return Result(keyParams);
        }
        /// <summary>
        /// 创建Google验证,并直接给用户绑定
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [RoleLevel(9), HttpGet]
        public async Task<SpActionResult<GoogleBindingModel>> Generate(Guid uid)
        {
            return await _googleAuth.GenerateBind(uid);
        }
    }
}
